import { message } from 'antd'
import { queryStaff, queryProfile1, queryProfile2, queryProfile3, projectFinish, projectRemark } from '../services/staff'

export default {
  namespace: 'staffHall',

  state: {
    visible: false,
    id: 0,
    profile: [],

    initLoading: true,
    loading: false,
    data: [],
    list: []
  },

  effects: {
    // 获取员工列表
    * fetchStaff({ payload = {} }, { call, put, select }) {
      const staffHall = yield select(_ => _.staffHall)

      yield put({
        type: 'save',
        payload: {
          loading: true,
          initLoading: false,
          list: staffHall.data.concat([...new Array(3)].map(() => ({ loading: true, name: {} }))),

          // eslint-disable-next-line
          staff_name: payload.staff_name ? payload.staff_name : staffHall.staff_name ? staffHall.staff_name : null
        },
      })

      // 搜索
      if (staffHall.staff_name && !payload.staff_name) {
        Object.assign(payload, {
          staff_name: staffHall.staff_name
        })
      }

      const { status = false, data = [] } = yield call(queryStaff, payload)
      if (status && data[0]) {
        const list = staffHall.data.concat(data)
        yield put({
          type: 'save',
          payload: { data: list, list, loading: false, },
        })
      } else {
        yield put({
          type: 'save',
          payload: { ...staffHall, loading: false, isDisable: true },
        })
      }
      window.dispatchEvent(new Event('resize'))
    },
    // 获取项目详情
    * fetchProfile({ payload = {} }, { call, put, select }) {
      let { id = 0 } = payload
      // 默认去进行中的项目
      const { index = 1 } = payload
      const staffHall = yield select(_ => _.staffHall)
      if (!id) {
        // eslint-disable-next-line
        id = staffHall.id
      }

      let uri = null
      switch (index) {
        case 1:
          uri = queryProfile1
          break
        case 2:
          uri = queryProfile2
          break
        case 3:
          uri = queryProfile3
          break
        default:
          uri = queryProfile1
          break
      }
      const { status = false, data = {} } = yield call(uri, { id })
      if (status) {
        yield put({
          type: 'save',
          payload: {
            ...payload,
            profile: data
          },
        })
      } else {
        message.error('获取详情失败')
      }
    },
    // 获取项目详情
    * changeTab({ payload = {} }, { put }) {
      yield put({
        type: 'save',
        payload: {
          ...payload,
          list: [],
          data: [],
          staff_name: null
        },
      })
      yield put({
        type: 'fetchStaff',
        payload,
      })
    },
    // 任务提交
    * projectFinish({ payload = {} }, { call, put }) {

      // projectFinish
      const { status = false } = yield call(projectFinish, payload)
      if (status) {
        message.success('任务提交成功')
        yield put({
          type: 'save',
          payload: {
            visible: false
          },
        })
      } else {
        message.success('任务提交失败')
      }

    },

    // 备注
    * projectRemark({ payload = {} }, { call, put }) {

      // projectFinish
      const { status = false } = yield call(projectRemark, payload)
      if (status) {
        message.success('任务备注成功')
        yield put({
          type: 'save',
          payload: {
            visible: false
          },
        })
      } else {
        message.success('任务备注失败')
      }

    },

  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(() => {
        // 路由发生改变时 清空数据
        dispatch({
          type: 'save',
          payload: {
            list: [],
            data: [],
            staff_name: null
          },
        })
      })
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload
      }
    },
  },
}